/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.enterprise.entity.PolicyFile;
import org.springblade.modules.enterprise.entity.PolicyInfo;
import org.springblade.modules.enterprise.mapper.PolicyInfoMapper;
import org.springblade.modules.enterprise.service.IPolicyFileService;
import org.springblade.modules.enterprise.service.IPolicyInfoService;
import org.springblade.modules.enterprise.vo.PolicyInfoVO;
import org.springblade.modules.enterprise.wrapper.PolicyInfoWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-08-19
 */
@AllArgsConstructor
@Service
public class PolicyInfoServiceImpl extends BaseServiceImpl<PolicyInfoMapper, PolicyInfo> implements IPolicyInfoService {
	private IPolicyFileService policyFileService;

	@Override
	public IPage<PolicyInfoVO> selectPolicyInfoPage(IPage<PolicyInfoVO> page, PolicyInfoVO policyInfo) {
		return page.setRecords(baseMapper.selectPolicyInfoPage(page, policyInfo));
	}

	@Override
	public IPage<PolicyInfoVO> selectPolicyInfoWithApplyStatus(IPage<PolicyInfoVO> page, PolicyInfoVO policyInfo) {
		return page.setRecords(baseMapper.selectPolicyInfoWithApplyStatus(page, policyInfo));
	}

	@Override
	public PolicyInfo getByIdWhetherIsDeleted(Long policy) {
		return baseMapper.selectPolicyInfoByIdWhetherIsDeleted(policy);
	}

	@Override
	public PolicyInfoVO getOne(Wrapper<PolicyInfo> queryWrapper, boolean throwEx) {
		PolicyInfo policyInfo = super.getOne(queryWrapper, throwEx);
		// 转换成VO类
		PolicyInfoVO policyInfoVO = PolicyInfoWrapper.build().entityVO(policyInfo);
		// 政策文件查询条件
		QueryWrapper<PolicyFile> fileQueryWrapper = new QueryWrapper<>();
		fileQueryWrapper.eq("policy", policyInfoVO.getId());
		// 获取政策文件
		policyInfoVO.setFiles(policyFileService.list(fileQueryWrapper));
		return policyInfoVO;
	}

	@Override
	@Transactional
	public boolean saveOrUpdate(PolicyInfo entity) {
		// 1.保存政策信息
		boolean policyInfo = super.saveOrUpdate(entity);
		// 2.清空政策下所有文件
		PolicyInfoVO policyInfoVO = (PolicyInfoVO) entity;
		Long policyInfoId = policyInfoVO.getId();
		policyFileService.remove(new QueryWrapper<PolicyFile>().eq("policy", policyInfoId));
		// 3.更新政策文件
		boolean policyFiles = true;
		List<PolicyFile> files = policyInfoVO.getFiles();
		if (Func.isNotEmpty(files)) {
			// 关联政策ID
			files.forEach(policyFile -> {
				// 用的是假删除,插入时ID会冲突,所以要把ID改为null
				policyFile.setId(null);
				// 关联政策信息
				policyFile.setPolicy(policyInfoId);
			});
			policyFiles = policyFileService.saveBatch(files);
		}
		return policyInfo && policyFiles;
	}
}
